package com.unity3d.dababinding;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.BindingAdapter;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableField;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.unity3d.dababinding.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding mainBinding;

    //String name;
    ObservableField<String>name = new ObservableField<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //改造setContentView
        mainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);

        //包含了viewBinding的功能
        //setContentView(R.layout.activity_main);
        //mainBinding.txtViewId.setText("HNUCM");

        /*1.数据绑定*/

        //mainBinding("HNUCM");
        name.set("https://profile.csdnimg.cn/5/2/B/1_weixin_53463734");
        mainBinding.setName(name);
        /*
        * 优势：
        * 之前的开发：取值-》操作UI
        * 操作UI交给布局文件
        * Activity只需要处理数据
        * */

        //绑定一个实体类
        //并没有操作UI，专注与数据的操作
        Person person=new Person();
        person.name="lixu";
        person.age=20;
        mainBinding.setPerson(person);

        /*2.事件绑定*/
        mainBinding.setMyclick(new Myclick());
    }

    /*定义一个内部类*/
    public class Myclick{
        public void onClick(View view){
            name.set("new HNUCM");
            Log.i("myclick", "onClick: "+"事件绑定成功");
        }

        public void onClick1(Person person){
            //在Activity中更新数据，并不会更新到布局文件中
            /*person.name="new lixu";
            person.age=21;*/
            person.setName("new lixu");
            person.setAge(21);
            Log.i("myclick", "onClick: 事件绑定成功"+person.name);
        }
    }

    //实际还是使用Glide
    @BindingAdapter("imageurl")
    public static void bindImageUrl(ImageView view,String url){
        Glide.with(view)
                .load(url)
                .into(view);
    }


}